package com.goddess.yeecolor.activity.myFragmentItemActivityAll;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import com.goddess.yeecolor.Modle.FirstBean;
import com.goddess.yeecolor.Modle.SecondBean;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.LoginAcitivity;
import com.goddess.yeecolor.adapter.ExpandableListViewAdapterOne;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.webview.PublicFragmentViewpagerWebView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.goddess.yeecolor.R.id.expand_list;
import static com.goddess.yeecolor.tools.MyUrl.SEEBUYLIST;
import static com.goddess.yeecolor.tools.MyUrl.TAG;

/**
 * 作者: CaoJunping
 * 时间: 2017/1/19
 * 作用: ******
 * 备注: 观看购买课程
 */
public class WatchActivity extends BaseActivity implements ExpandableListView.OnGroupExpandListener {
    private ExpandableListView mList;
    private ExpandableListViewAdapterOne mAdapter;
    private View back_view;
    private ImageView img;
    private View empty;

    private Map<Object,Object> map;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_watch);

        initData();

        initFindView();

        initOnClck();

    }
    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void initOnClck() {
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }

    @Override
    public void initFindView() {
        mList = (ExpandableListView) findViewById(expand_list);
        img= (ImageView) findViewById(R.id.img_back);
        empty=findViewById(R.id.empty);

        //设置点击父控件的监听
        mList.setOnGroupExpandListener(this);
    }

    /**
     * 初始化数据
     */
    @Override
    public void initData() {
        String url=SEEBUYLIST;
        map=new HashMap<>();
        map.put("unique", SharePreceUtils.getOther("user","unique"));
        Log.e(TAG, SharePreceUtils.getOther("user","unique"));
        OkttpUtils.getIntence().OkttpPost(url,map,handler,1);
    }
    ArrayList<FirstBean> mDatas = new ArrayList<FirstBean>();
    ArrayList<SecondBean> mArrlistSecondBean;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                String jsonString =msg.getData().getString("result");
                //第一层菜单数据
                JSONObject jsonObject=new JSONObject(jsonString);
                int code=jsonObject.getInt("code");
                if(code==100){
                    JSONArray jsonArray=jsonObject.getJSONArray("result");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject1=jsonArray.getJSONObject(i);
                        FirstBean firstBean = new FirstBean();
                        firstBean.setTitle(jsonObject1.getString("add_date"));
                        //获取第二层菜单的数据
                        mArrlistSecondBean= new ArrayList<SecondBean>();
                        JSONArray jsonArray1=jsonObject1.getJSONArray("list");
                        for (int j=0;j<jsonArray1.length();j++){

                            SecondBean secondBean = new SecondBean();
                            JSONObject jsonObject2=jsonArray1.getJSONObject(j);
                            JSONObject jsonObject3=jsonObject2.getJSONObject("events");

                            secondBean.setImg(jsonObject3.getString("thumb"));
                            secondBean.setTitle(jsonObject3.getString("title"));
                            secondBean.setState(jsonObject3.getString("state"));
                            secondBean.setDate(jsonObject3.getString("start_date"));
                            secondBean.setWebview(jsonObject3.getString("webview"));
                            secondBean.setId(jsonObject3.getString("id"));
                            secondBean.setPrice(jsonObject3.getString("price"));

                            mArrlistSecondBean.add(secondBean);
                        }
                        firstBean.setSecondBeen(mArrlistSecondBean);
                        mDatas.add(firstBean);
                    }
                }else if(code==403){
                    empty.setVisibility(View.VISIBLE);
                    mList.setVisibility(View.GONE);
                }
                mAdapter = new ExpandableListViewAdapterOne(WatchActivity.this,mDatas);
                mList.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
                for (int i = 0; i < mAdapter.getGroupCount(); i++) {
                    mList.expandGroup(i);
                }

                mAdapter.setOnItemClickListener(new ExpandableListViewAdapterOne.OnItemClickListener() {
                    @Override
                    public void OnItemClick(View view,int parentPosition, int childPosition) {
                        String uuid= SharePreceUtils.getOther("user","unique");
                        if(!"".equals(uuid)){
                            Intent intent=new Intent();
                            intent.setClass(WatchActivity.this,PublicFragmentViewpagerWebView.class);
                            intent.putExtra("id",String.valueOf(mDatas.get(parentPosition).getSecondBeen().get(childPosition).getId()));
                            intent.putExtra("webview",mDatas.get(parentPosition).getSecondBeen().get(childPosition).getWebview());
                            intent.putExtra("price",mDatas.get(parentPosition).getSecondBeen().get(childPosition).getPrice());
                            startActivity(intent);
                        }else{
                            Intent intent=new Intent(WatchActivity.this,LoginAcitivity.class);
                            startActivity(intent);
                        }
                    }
                });

                Log.e(TAG,jsonString);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };


    @Override
    public void onGroupExpand(int groupPosition) {

    }
}
